package base.httputils;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;

import java.io.File;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import utils.ToastUtils;

/**
 * Created by My on 2017/10/16.
 */

public class HttpClient {

    /**
     * 下载文件
     *
     * @param context
     * @param downloadUrl 文件下载地址
     * @param floder      文件保存的本地路径
     * @param fileName    文件保存的文件名
     * @param callBack    下载回调
     */
    public static void downloadFile(Context context, String downloadUrl, String floder, String fileName, final OnDownloadCallBack callBack) {
        OkGo.<File>get(downloadUrl)
                .tag(context)
                .execute(new FileCallback(floder, fileName) {
                    @Override
                    public void onSuccess(Response<File> response) {
                        callBack.success(response.body());
                    }

                    @Override
                    public void downloadProgress(Progress progress) {
                        super.downloadProgress(progress);
                        callBack.progress(progress);
                    }

                    @Override
                    public void onError(Response<File> response) {
                        super.onError(response);
                        response.getException().printStackTrace();
                        callBack.failed("下载失败，请稍后重试");
                    }
                });
    }
    /**
     * 获取jsonResult信息
     * @return
     */
    public static void getHttp(final Context ctx, String name , Map<String,Object> map, final HttpCallBack httpcallBack) {

//        Toast.makeText(ctx, map.toString(), Toast.LENGTH_LONG).show();
//        Log.e("afasf",map.toString());
//        Log.e("afasf",map.toString());
       HttpParams httpParams =new HttpParams();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            httpParams.put(key,value);
        }

        OkGo.<String>get(name)//
                .tag(ctx)//
//                .upJson(new Gson().toJson(map))
                .params(httpParams)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        //注意这里已经是在主线程了


                        try {
                            if (response!=null){
                                String data = response.body();//这个就是返回来的结果
                                httpcallBack.onSuccess(data);
                            }else{
                                httpcallBack.onError("");
                            }
                        }catch (Exception e){
//                            Log.e("12131","错粗了::"+response.body());
                            httpcallBack.onError("");
                        }


                    }
                    @Override
                    public void onError(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        super.onError(response);
                    }
                });
    }

    public static void postHttp(final Context ctx, String name,Map<String,Object> map, final HttpCallBack httpcallBack) {
        HttpParams httpParams =new HttpParams();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            httpParams.put(key,value);
        }

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, new Gson().toJson(map));
        OkGo.<String>post(name)//
                .tag(ctx)//
                .upRequestBody(body)

//                .upJson(new Gson().toJson(map))
//                .params(httpParams)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        //注意这里已经是在主线程了

                        try {
                            if (response!=null){
                                String data = response.body();//这个就是返回来的结果
                                httpcallBack.onSuccess(data);
                            }else{
                                httpcallBack.onError("");
                            }
                        }catch (Exception e){
//                            Log.e("12131","错粗了::"+response.body());
                            httpcallBack.onError("");
                        }


                    }
                    @Override
                    public void onError(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        super.onError(response);
                    }
                });
    }


    public static void postwxHttp(final Context ctx, String name,Map<String,Object> map, final HttpCallBack httpcallBack) {
        HttpParams httpParams =new HttpParams();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            httpParams.put(key,value);
        }

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, new Gson().toJson(map));
        OkGo.<String>post(name)//
                .tag(ctx)//
                .upRequestBody(body)

//                .upJson(new Gson().toJson(map))
//                .params(httpParams)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        //注意这里已经是在主线程了


                        try {
                            if (response!=null){
                                String data = response.body();//这个就是返回来的结果
                                Result result= JSONUtils.parserObject(data,Result.class);
                                if (result.getCode()==0){
                                    httpcallBack.onSuccess(data);
                                }else {
                                    ToastUtils.showToast(ctx,result.getMsg());
                                }

                            }else{
                                httpcallBack.onError("");
                            }
                        }catch (Exception e){
//                            Log.e("12131","错粗了::"+response.body());
                            httpcallBack.onError("");
                        }


//                        if (response!=null){
//                            String data = response.body();//这个就是返回来的结果
//                            Result result= JSONUtils.parserObject(data,Result.class);
//                            if (result.getCode()==0){
//                                httpcallBack.onSuccess(data);
//                            }else {
//                                ToastUtils.showToast(ctx,result.getMsg());
//                            }
//
//                        }else{
//                            httpcallBack.onError("");
//                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        super.onError(response);
                    }
                });
    }

    public static void postHttpAndHearder(final Context ctx, String name, String token,Map<String,Object> map, final HttpCallBack httpcallBack) {
////        ProgressDialogHttp.showProgressDialog(ctx);
//        Toast.makeText(ctx, map.toString(), Toast.LENGTH_LONG).show();
//        Log.e("afasf",map.toString());
//        Log.e("afasf",map.toString());
        map.put("token",token);
        HttpParams httpParams =new HttpParams();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey().toString();
            String value = entry.getValue().toString();
            httpParams.put(key,value);
        }

        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, new Gson().toJson(map));
        OkGo.<String>post(name)//
                .tag(ctx)//

                .upRequestBody(body)

                .headers("Authorization","Bearer "+token)
//                .isMultipart(true)
//                .upJson(new Gson().toJson(map))
//                .params(httpParams)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        //注意这里已经是在主线程了
                        try {
                            if (response!=null){
                                String data = response.body();//这个就是返回来的结果
                                Result result= JSONUtils.parserObject(data,Result.class);
                                if (result.getCode()==0||result.getCode()==200){
                                    httpcallBack.onSuccess(data);
                                }else {
                                    ToastUtils.showToast(ctx,result.getMsg());
                                }

                            }else{
                                httpcallBack.onError("");
                            }
                        }catch (Exception e){
//                            Log.e("12131","错粗了::"+response.body());
                            httpcallBack.onError("");
                        }

                    }
                    @Override
                    public void onError(Response<String> response) {
//                        ProgressDialogHttp.disMissDialog();
                        super.onError(response);
                    }
                });
    }
}
